package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IUserService userService;

    private static final String FOLLOW_KEY = "follow:";


    @Override
    public Result follow(Long followUserId, boolean isFollow) {

        // 获取当前用户的id
        Long currentUserId = UserHolder.getUser().getId();

        String key = FOLLOW_KEY + currentUserId;

        // 是关注还是取关
        if(isFollow) {
            Follow follow = new Follow();
            follow.setFollowUserId(followUserId);
            follow.setUserId(currentUserId);
            follow.setCreateTime(LocalDateTime.now());
            boolean save = save(follow);

            if(save) {
                // 插入到set集合
                redisTemplate.opsForSet().add(key, followUserId + "");
            }

        } else {

            // 找到数据库的数据，然后删掉
            boolean remove = remove(new QueryWrapper<Follow>()
                    .eq("user_id", currentUserId)
                    .eq("follow_user_id", followUserId));
            if(remove) {
                // 取关
                redisTemplate.opsForSet().remove(key, followUserId + "");
            }
        }

        return Result.ok();
    }

    @Override
    public Result followOrNot(Long followUserId) {
        // 获取当前用户的id
        Long currentUserId = UserHolder.getUser().getId();

        String key = FOLLOW_KEY + currentUserId;

        Boolean member = redisTemplate.opsForSet().isMember(key, followUserId + "");

        return Result.ok(member);
    }

    @Override
    public Result commonFollow(Long blogUserId) {

        // 获取登录用户
        Long currentUserId = UserHolder.getUser().getId();

        // 获取当前用户的关注列表key
        String currentUserKey = FOLLOW_KEY + currentUserId;

        // 博主的关注列表key
        String blogUserKey = FOLLOW_KEY + blogUserId;

        // 取交集
        Set<String> intersect = redisTemplate.opsForSet().intersect(currentUserKey, blogUserKey);
        if(intersect != null && intersect.size() > 0) {

            List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());

            List<UserDTO> users = userService.listByIds(ids)
                    .stream()
                    .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                    .collect(Collectors.toList());

            return Result.ok(users);
        }
        return Result.ok(Collections.emptyList());
    }
}
